package lang

import (
	"JVM/Jvmgo/GoToJVM/native"
	"JVM/Jvmgo/GoToJVM/runtimeData"
	"math"
)

// Math类初始化需要调用
func init() {
	native.Register("java/lang/Float",
		"floatToRawIntBits", "(F)I", floatToRawIntBits)
	native.Register("java/lang/Float", "intBitsToFloat", "(I)F", intBitsToFloat)
}

// 将一个浮点数转换为其原始的 IEEE 754 浮点数表示的整数形式。
// public static native int floatToRawIntBits(float value)
func floatToRawIntBits(frame *runtimeData.Frame) {
	value := frame.LocalVars().GetFloat(0)
	bits := math.Float32bits(value)
	frame.OperandStack().PushInt(int32(bits))
}

// public static native float intBitsToFloat(int bits);
// (I)F
func intBitsToFloat(frame *runtimeData.Frame) {
	bits := frame.LocalVars().GetInt(0)
	value := math.Float32frombits(uint32(bits)) // todo
	frame.OperandStack().PushFloat(value)
}
